package com.myjob.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myjob.config.Constants;
import com.myjob.entity.Blog;
import com.myjob.mapper.BlogMapper;
import com.myjob.response.Result;
import com.myjob.service.BlogService;
import com.myjob.utils.RedisUtil;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Chen Yuan
 * @create 2021-01-21  19:14
 */
@Service
@Slf4j
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询 博客信息
     * @param userName 用户名
     * @param blogTitle 博客名
     * @param deleted 是否逻辑删除
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return 封装好的结果
     */
    @Override
    public Result selectBlog(String searchValue,String userId, String userName, String blogTitle,
                             Integer deleted,Integer currentPage, Integer pageSize) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        // 判断参数是否为空
        if (UtilValidate.isNotEmpty(searchValue)){
            wrapper.and(wrapper1 -> wrapper1.like("user_name", searchValue).
                    or().like("blog_title", searchValue).
                    or().like("blog_content", searchValue));
        }
        if (UtilValidate.isNotEmpty(userId)){
            wrapper.eq("user_id", userId);
        }
        if (UtilValidate.isNotEmpty(userName)){
            wrapper.like("user_name", userName);
        }
        if (UtilValidate.isNotEmpty(blogTitle)){
            wrapper.like("blog_title", blogTitle);
        }
        wrapper.eq("deleted", deleted);
        wrapper.orderByDesc("update_time");
        Integer size = UtilValidate.isEmpty(pageSize) ? Constants.getPageSize() : pageSize;
        Page<Blog> page = new Page<>(currentPage, size);
        IPage<Blog> blogs = blogMapper.selectPage(page, wrapper);

        if (UtilValidate.isEmpty(blogs.getRecords())){
            if (blogs.getCurrent() != 1){
                log.info(" == 第 "+ blogs.getCurrent() +" 页没有数据，查询到最后一页。");
                page  = new Page<>(blogs.getPages(), size);
                blogs = blogMapper.selectPage(page, wrapper);
            }
        }
        return Result.succeed(blogs);
    }

    /**
     * 查询热点博客
     * @param number 查询阅读量排在前几的
     * @return
     */
    @Override
    public Result selectHotBlog(String number) {
        if (UtilValidate.isEmpty(number)){
            number = "4";
        }
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("read_number").
                last("LIMIT 0,"+number);
        List<Blog> blogs = blogMapper.selectList(wrapper);

        return Result.succeed(blogs,"成功查询前"+number+"条数据");
    }

    @Override
    public Result selectBlogById(Long blogId) {
        if (UtilValidate.isEmpty(blogId)){
            return Result.failed("参数为空");
        }

        Blog blog = blogMapper.selectById(blogId);
        if (UtilValidate.isEmpty(blog)) {
            return Result.failed("查询不到对应数据");
        }
        return Result.succeed(blog,"成功查询数据");
    }

    /**
     * 添加博客
     * @param blog 博客内容
     * @return 影响行数
     */
    @Override
    public int addBlog(Blog blog) {
        // 如果参数为空，则直接返回
        if (UtilValidate.isEmpty(blog)){
            return -1;
        }
        blog.setDeleted(Constants.getNoDel());
        return blogMapper.insert(blog);
    }

    /**
     * 修改博客
     * @param blog 博客内容
     * @return 影响行数
     */
    @Override
    public int updateBlog(Blog blog) {
        // 如果参数为空，则直接返回
        if (UtilValidate.isEmpty(blog)){
            return -1;
        }
        return blogMapper.updateById(blog);
    }

    /**
     * 删除博客
     * @param blogId 博客编号
     * @return 封装的结果
     */
    @Override
    public int deleteBlog(Long blogId) {
        if (UtilValidate.isEmpty(blogId)){
            return -1;
        }
        Blog blog = blogMapper.selectById(blogId);
        if (UtilValidate.isEmpty(blog)){
            return -1;
        }
        int row = 0;
        if (Objects.equals(blog.getDeleted(), Constants.getNoDel())){
            // 逻辑删除
            blog.setDeleted(Constants.getYetDel());
            row = blogMapper.updateById(blog);
            if (row == 1){
                row = 10;
            }
        }else {
            // 物理删除
            row = blogMapper.deleteById(blogId);
            if (row == 1){
                row = 20;
            }
        }
        return row;
    }

}
